package com.sendi.common.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.opensymphony.xwork2.ActionContext;

public class CacheManager {
	public static HttpServletRequest getRequest() {
		ActionContext ctx = ActionContext.getContext();
		HttpServletRequest request = (HttpServletRequest)ctx.get("com.opensymphony.xwork2.dispatcher.HttpServletRequest");
		return request;
	}

	public static HttpServletResponse getResponse() {
		ActionContext ctx = ActionContext.getContext();
		HttpServletResponse response = (HttpServletResponse)ctx.get("com.opensymphony.xwork2.dispatcher.HttpServletResponse");
		return response;
	}
	  
	public static HttpSession getSession(){
		return getRequest().getSession();
	}
	
	public static boolean isExist(String attrName){
		return getSession().getAttribute(attrName) != null;
	}
	
	public static boolean isCache(String attrName,Object obj){
		if(obj == null || !isExist(attrName))
			return false;
		Object co = getSession().getAttribute(attrName);
		return co.equals(obj);
	}
	public static void cache(String attrName,Object obj){
		getSession().setAttribute(attrName, obj);
	}
	public static Object getCache(String attrName){
		return getSession().getAttribute(attrName);
	}
	public static boolean isStartWith(String attrName,String prefix){
		if(attrName == null || attrName.length() == 0 //属性名为空
				|| prefix == null || prefix.length() == 0 //前缀为空
				|| !isExist(attrName))//属性不存在
			return false;
		return getSession().getAttribute(attrName).toString().startsWith(prefix);
	}
}
